﻿using System;
using System.Collections.Generic;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace Utility
{
    public class CrystalReportHelper
    {
        public static CrystalReportHelper Instance = new CrystalReportHelper();
        public static string outputFullPath = string.Empty;

        private CrystalReportHelper() { }

        public string GetPath(string filename)
        {
            filename = filename.Replace("/", "\\");
            if (filename[0] == '\\')
                filename = filename.Remove(0, 1);
            filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
            return filename;
        }

        public string GetPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        public string GetServerPath(string filename)
        {
            string rootPath = Path.GetPathRoot(filename);
            filename = filename.Replace("/", "\\");
            if (rootPath.Contains(":"))
            {
                filename = filename.Replace(rootPath, "");
            }

            return GetPath(filename);
        }

        public bool ExportToDisk(IDictionary<string, object> formParams, string rptfilename, string outputfilename, string type, string server, string database, string user, string password, bool isCover, params string[] keys)
        {
            ReportDocument rpt = new ReportDocument();

            rptfilename = GetServerPath(rptfilename);
            outputfilename = GetServerPath(outputfilename);

            rpt.Load(rptfilename);

            foreach (IConnectionInfo connection in rpt.DataSourceConnections)
                connection.SetConnection(server, database, user, password);

            foreach (string key in keys)
                rpt.SetParameterValue("@p" + key, formParams[key].ToString());

            string path = Path.GetDirectoryName(outputfilename);

            if (!isCover)
            {
                if (!File.Exists(outputfilename))
                {
                    outputFullPath = outputfilename;
                    return true;
                }
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (type.Equals("pdf"))
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, outputfilename);

                outputFullPath = outputfilename;
            }

            else if (type.Equals("excel"))
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, outputfilename);
            }
            else if (type.Equals("xml"))
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Xml, outputfilename);
            }
            else if (type.Equals("html40"))
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.HTML40, outputfilename);
            }
            rpt.Close();
            return true;
        }
    }
}